import Vue from 'vue'
import VueRouter from 'vue-router'

import LoginView from "@/user/LoginView";
import MainView from "@/user/MainView";
import WelcomeView from "@/user/WelcomeView";
import {Message} from "element-ui";
import RoleView from "@/user/RoleView";

Vue.use(VueRouter)

const routes = [
    {path: '/', name: 'LoginView', component: LoginView},
    {
        path: '/main',
        name: 'MainView',
        component: MainView,
        children: [
            {path: '/welcome', name: 'WelcomeView', component: WelcomeView},
            {path: '/role/select', name: 'RoleView', component: RoleView},

        ]
    }
]

const router = new VueRouter({
    routes
})

// 添加一个路由守卫（不登录不能访问内部资源）
router.beforeEach((to, from, next) => {

    let token = sessionStorage.getItem("token");
    console.log("路由守卫中的token：",token);

    if (to.name !== 'LoginView' && token == null) {
        Message({
            type: "error",
            message: "对不起,请先登录!",
            duration: 1000
        })

        next({name: 'LoginView'})

    } else {
        next()
    }
})


export default router
